<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Category;
use App\lib\TreeClass;
use App\Model\Goods;

class GoodsController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $keyword = $request->keyword;
        $map = [];
        if (!empty($keyword)) {
            $map[] = ['title', 'like', "%{$keyword}%"];
        }
        $goodsDatas = Goods::where($map)->paginate(5);
        return view('admin.goods.index', compact('goodsDatas'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categorys = Category::all()->toArray();
        $categorys = TreeClass::tree($categorys, $title = 'name', $fieldPri = 'id', $fieldPid = 'pid');
        return view('admin.goods.create', compact('categorys'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, Goods $goods)
    {
        if ($request->isMethod('post')) {
            $data = $request->all();
            //截取第一张图片为封面
            $data['list_img'] = current(explode('|', $data['imgs']));
            unset($data['file']);
            $result = $goods->create($data);
            if ($result) {
                return redirect('admin/goods')->with('status', '操作成功');
            } else {
                return redirect('admin/goods')->with('status', '操作失败');
            }
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $categorys = Category::all()->toArray();
        $categorys = TreeClass::tree($categorys, $title = 'name', $fieldPri = 'id', $fieldPid = 'pid');
        $goodsData = Goods::find($id);
        return view('admin.goods.edit', compact('goodsData', 'categorys'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->all();
        //截取第一张图片为封面
        $data['list_img'] = current(explode('|', $data['imgs']));
        unset($data['_method']);
        unset($data['_token']);
        unset($data['file']);
        $res = Goods::where('id', $id)->update($data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Goods::where('id', $id)->delete();
        return redirect('/admin/goods')->with('status', '删除成功');
    }
}
